package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.dao.SysDictDataDao;
import com.open.capacity.user.model.SysDictData;
import com.open.capacity.user.service.SysDictDataService;
import com.open.capacity.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class SysDictDataServiceImpl implements SysDictDataService {

    @Autowired
    private SysDictDataDao dictDataDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysDictData sysDictData) {
        sysDictData.setCreateTime(new Date());
        sysDictData.setUpdateTime(sysDictData.getCreateTime());

        dictDataDao.save(sysDictData);
        log.info("保存字典数据：{}", sysDictData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysDictData sysDictData) {
        sysDictData.setUpdateTime(new Date());

        dictDataDao.updateByOps(sysDictData);
        log.info("修改字典数据：{}", sysDictData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        SysDictData sysDictData = dictDataDao.findById(id);

        dictDataDao.delete(id);

        log.info("删除字典数据：{}", sysDictData);
    }


    @Override
    public SysDictData findById(String id) {
        return dictDataDao.findById(id);
    }

    @Override
    public String findNameByValue(String code, String value) {
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        params.put("value", value);
        List<SysDictData> list = dictDataDao.findList(params);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0).getName();
        }
        return "";
    }

    @Override
    public List<SysDictData> findDictDatas(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<SysDictData> list = dictDataDao.findList(params);
        return list;
    }

    @Override
    public List<SysDictData> findDictDatas(String code) {
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        List<SysDictData> list = dictDataDao.findList(params);
        return list;
    }

    @Override
    public List<SysDictData> findByParentId(String parentId) {
        return dictDataDao.findByParentId(parentId);
    }


    @Override
    public Result saveOrUpdate(SysDictData sysDictData, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            SysDictData model = dictDataDao.findById(keyValue);
            if (model != null) {
                sysDictData.setId(keyValue);
                sysDictData.setUpdateTime(new Date());
                i = dictDataDao.updateByOps(sysDictData);
            }
        } else {
            sysDictData.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            sysDictData.setCreateTime(new Date());
            sysDictData.setUpdateTime(sysDictData.getCreateTime());
            i = dictDataDao.save(sysDictData);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
